#include "TaskState.h"

#include "..\EnumMapping.h"
#include "..\Util.h"

namespace BroodWar
{
	namespace Addon
	{
		TaskState^ ConvertTaskState(BWSAL::TaskState state)
		{
			return gcnew TaskState(state);
		}

		BWSAL::TaskState ConvertTaskState(TaskState^ state)
		{
			if(state == nullptr)
				return BWSAL::TaskState();
			return BWSAL::TaskState(*(state->instance));
		}

		TaskState::TaskState(BWSAL::TaskState state)
		{
			instance = new BWSAL::TaskState(state);
			dispose = true;
		}
		
		TaskState::TaskState(BWSAL::TaskState* state)
		{
			instance = state;
			dispose = false;
		}
		
		TaskState::~TaskState()
		{
			if(dispose)
				delete instance;
		}
		
		TaskState::!TaskState()
		{
			if(dispose)
				delete instance;
		}

		TaskState::TaskState()
		{
			instance = new BWSAL::TaskState();
			dispose = true;
		}
		
		TaskState::TaskState(int id)
		{
			instance = new BWSAL::TaskState(id);
			dispose = true;
		}

		int TaskState::Id::get()
		{
			return instance->getID();
		}

		TaskStateType TaskState::Type::get()
		{
			return EnumMapping::TaskStateType->Managed(instance);
		}

		bool TaskState::TypeEquals(TaskStateType buildType)
		{
			return Type == buildType;
		}

		bool TaskState::IsWaiting::get()
		{
			return instance->isWaiting();
		}

		bool TaskState::IsRunning::get()
		{
			return instance->isRunning();
		}

		bool TaskState::IsCompleted::get()
		{
			return instance->isCompleted();
		}

		int TaskState::GetHashCode()
		{
			return instance->getID();
		}

		bool TaskState::Equals(Object^ o)
		{
			TaskState^ other = dynamic_cast<TaskState^>(o);
			return this->Equals(other);
		}

		String^ TaskState::ToString()
		{
			return gcnew String(instance->getName().c_str());
		}

		bool TaskState::Equals(TaskState^ other)
		{
			if(ReferenceEquals(nullptr, other))
				return false;
			if(ReferenceEquals(this, other))
				return true;
			return this->instance->getID() == other->instance->getID();
		}

		HashSet<TaskState^>^ TaskState::AllTaskStates::get()
		{
			return ToHashSet<BWSAL::TaskState, TaskState^>(BWSAL::TaskStates::allTaskStates(), &ConvertTaskState);
		}

		bool TaskState::operator == (TaskState^ first, TaskState^ second)
		{
			if(ReferenceEquals(first, second))
				return true;
			if(ReferenceEquals(nullptr, first))
				return false;
			return first->Equals(second);
		}

		bool TaskState::operator != (TaskState^ first, TaskState^ second)
		{
			return !(first == second);
		}
	}
}
